package git.soulbgm.job;

import git.soulbgm.common.pojo.Response;
import git.soulbgm.common.enums.CodeEnum;
import git.soulbgm.service.AlarmInfoService;
import git.soulbgm.websocket.AlarmWebSocketServer;
import git.soulbgm.websocket.MonitorWebSocket;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import static git.soulbgm.common.MonitorConstants.ALARM_COUNT_CACHE_EXPIRE_TIME;

/**
 * 发送监控数据
 *
 * @author SoulBGM
 * @date 2023-04-18
 */
@Service
public class WebSocketPushData {

    private final AlarmInfoService alarmInfoService;

    public WebSocketPushData(AlarmInfoService alarmInfoService) {
        this.alarmInfoService = alarmInfoService;
    }

    @Scheduled(initialDelay = 1000, fixedDelay = 5000)
    public void sendMonitorData() {
        if (!MonitorWebSocket.getSessionPools().isEmpty()) {
            MonitorWebSocket.broadcast(MonitorWebSocket.getMonitorData());
        }
    }

    @Scheduled(fixedDelay = ALARM_COUNT_CACHE_EXPIRE_TIME * 1000)
    public void timingPushAlarmCount() {
        if (!AlarmWebSocketServer.getSessionPools().isEmpty()) {
            AlarmWebSocketServer.broadcast(Response.setOk(CodeEnum.SUCCESS, alarmInfoService.getAlarmCountVo()));
        }
    }

}
